package com.itheima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.dto.ChannelPageRequestDto;
import com.itheima.admin.mapper.AdChannelMapper;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.dto.PageRequestDto;
import com.itheima.common.vo.PageResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.admin.service.impl
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Autowired
    private AdChannelMapper adChannelMapper;
    /**
     * 查询频道列表
     *
     * @param dto
     * @return
     */
    @Override
    public PageResultVo findPage(ChannelPageRequestDto dto) {
        //1. 设置分页参数
        Page<AdChannel> pageInfo = new Page<>(dto.getPage(), dto.getSize());
        //2. 构建查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        // 按名称 模糊 查询
        queryWrapper.like(!StringUtils.isEmpty(dto.getName()), AdChannel::getName, dto.getName());
        // 按状态 精确
        queryWrapper.eq(null != dto.getStatus(), AdChannel::getStatus, dto.getStatus());
        //3. 分页查询
        page(pageInfo, queryWrapper);
        //IPage<AdChannel> pageResult = page(pageInfo, queryWrapper);
        //System.out.println("pageResult==pageInfo?===>" + (pageResult==pageInfo));
        // 获取total
        long total = pageInfo.getTotal();
        // 获取分页结果
        List<AdChannel> list = pageInfo.getRecords();
        //4. 构建返回的结果
        return PageResultVo.pageResult(dto.getPage(),dto.getSize(),total,list);
    }

    /**
     * 新增频道
     *
     * @param adChannel
     */
    @Override
    public void add(AdChannel adChannel) {
        adChannel.setCreatedTime(new Date());
        adChannel.setIsDefault(false);
        save(adChannel);
    }
}
